//
//  RBSplashVC.m
//  zhongzhenglu
//
//  Created by XL on 2022/9/16.
//

#import "RBSplashVC.h"
#import <Masonry.h>
#import "RBDefine.h"
#import "RanTool.h"
#import "UIView+RBAdd.h"
#import "RBTimer.h"
#import "RBWebViewController.h"
#import "RBResponse.h"
#import "UIViewController+RBAdd.h"
#import "UIImageView+RBAdd.h"
#import "RBAppDelegateHelper.h"

@implementation RBSplashModel
@end

@implementation RBSplashResponse
@end

@interface RBSplashVC ()

@property(nonatomic, strong)UIImageView *imgView;
@property(nonatomic, strong)UIView *countView;
@property(nonatomic, strong)UILabel *secondL;

@property(nonatomic, strong)RBTimer *timer;

@end

@implementation RBSplashVC

#pragma mark - Lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setup];
    [self fillData];
}

#pragma mark - Private

- (void)setup
{
    self.view.backgroundColor = UIColor.whiteColor;
}

- (void)fillData
{
    [self.imgView setImageWithUrl:self.response.data.picurl placeHolder:nil];
    self.countView.hidden = NO;
    __weak typeof(self) weakSelf = self;
    NSInteger count = 5;
    [self.timer startTimerWithMaxCount:count interval:1 block:^(float countF, NSInteger countI) {
        weakSelf.secondL.text = [NSString stringWithFormat:@"%zdS",countI];
        if (countI <= 0)
        {
            [weakSelf jumpViewTapped];
        }
    }];
}

#pragma mark - Network

#pragma mark - Event

- (void)imgViewTapped
{
    if (self.response)
    {
        RBSplashModel *model = self.response.data;
        if (model.linkurl)
        {
            UITabBarController *tabVc = (UITabBarController *)[RBAppDelegateHelper keyWindow].rootViewController;
            if ([tabVc isKindOfClass:UITabBarController.class])
            {
                UINavigationController *naviVc = (UINavigationController *)tabVc.selectedViewController;
                if ([naviVc isKindOfClass:UINavigationController.class])
                {
                    RBWebViewController *vc = RBWebViewController.new;
                    vc.naviTitle = @"详情";
                    vc.urlPath = model.linkurl;
                    [naviVc pushViewController:vc animated:YES];
                }
            }
        }
        [self jumpViewTapped];
    }
}

- (void)jumpViewTapped
{
    [UIView animateWithDuration:0.3 animations:^{
        self.view.alpha = 0;
    } completion:^(BOOL finished) {
        [self dismissViewControllerAnimated:NO completion:nil];
    }];
}

#pragma mark - Setter

#pragma mark - Getter

- (RBTimer *)timer
{
    if (!_timer)
    {
        self.timer = RBTimer.new;
    }
    return _timer;
}

- (UIImageView *)imgView
{
    if (!_imgView)
    {
        self.imgView = ({
            UIImageView *imgView = UIImageView.new;
            imgView.contentMode = UIViewContentModeScaleAspectFill;
            [imgView rbAddTapGesWithTarget:self action:@selector(imgViewTapped)];
            [self.view addSubview:imgView];
            [imgView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.edges.mas_equalTo(0);
            }];
            imgView;
        });
    }
    return _imgView;
}

- (UIView *)countView
{
    if (!_countView)
    {
        self.countView = ({
            UIView *view = UIView.new;
            [view rbAddTapGesWithTarget:self action:@selector(jumpViewTapped)];
            view.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
            [view rbSetCornerRadius:Zoom(15)];
            [self.view addSubview:view];
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.bottom.mas_equalTo(-MAX(kRBSafeBottomHeight, Zoom(40)));
                make.right.mas_equalTo(-Zoom(15));
                make.height.mas_equalTo(Zoom(30));
                make.width.mas_equalTo(Zoom(100));
            }];
            
            [view h_view_addView:({
                UILabel *view = [RanTool labelWithFontSize:Zoom(14) weight:0 color:UIColor.whiteColor align:1 text:@"跳过"];
                [view addPriority:1];
                view;
            }) withHeight:CGFLOAT_MIN width:Zoom(50) top:0 bottom:0 left:0 right:CGFLOAT_MIN centerY:0];
            [view h_view_addView:({
                UIView *view = UIView.new;
                view.backgroundColor = UIColor.whiteColor;
                view;
            }) withHeight:CGFLOAT_MIN width:0.5 top:Zoom(8) bottom:Zoom(8) left:0 right:0 centerY:0];
            [view h_view_addView:({
                UILabel *view = [RanTool labelWithFontSize:Zoom(15) weight:0 color:UIColor.whiteColor align:1 text:@""];
                self.secondL = view;
                view;
            }) withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:0 bottom:0 left:0 right:Zoom(4) centerY:0];
            
            view;
        });
    }
    return _countView;
}

@end
